<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class PasswordRequset extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }
    public function boot()
    {
//      自定义的验证闭包接收四个参数：要被验证的属性名称 $attribute、属性的值 $value、传入验证规则的参数数组 $parameters、及 Validator 实例。
        \Validator::extend('check_password', function ($attribute, $value, $parameters, $validator) {
            return \Hash::check($value,\Auth::user()->password);
        });
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        $this->boot();
        return [
            'username'=>'required',
            'oldpassword'=>'nullable|check_password',
            'password'=>'nullable|confirmed|between:6,12',
            'password_confirmation'=>'nullable|between:6,12'
        ];
    }
    public function messages()
    {
        return [
          'oldpassword.check_password'=>'旧密码输入错误',
        ];
    }
}
